package ControllerLayer;

import java.util.Timer;
import java.util.TimerTask;

import ModelLayer.Game;
import ModelLayer.Menu;
import RenderLayer.TitleView;
import RenderLayer.View;
import ServiceLayer.Input;
import android.content.Context;
import android.media.MediaPlayer;

import com.example.genericrpg_android.GameEngine;
import com.example.genericrpg_android.R;

public class TitleMenuController extends Controller {
	Context activity;
	Menu mMenu;
	GameEngine mEngine;
	Game mModel;
	private TitleView mTitleView;
	MediaPlayer player;
	
	public TitleMenuController(Menu menu, Game model, GameEngine engine, Context activity){
		this.activity = activity;
		player = MediaPlayer.create(activity, R.raw.ff2prlde);
		mModel = model;
		mMenu = menu;
		mEngine = engine;
		mTitleView = new TitleView(menu, activity);
	}
	
	@Override
	public void handleInput(int input) {
		// TODO Auto-generated method stub
		switch(input){		
		case Input.DOWN: 
			mMenu.nextOption();
			break;
		case Input.UP:
			mMenu.previousOption();
			break;
		case Input.A:
			if (mMenu.getOption().equals("Start New Game")) {
				player.stop();
				Timer timer;
				TimerTask task = new TimerTask() {
					// Delays the loading of a room (allowing the view to fade in and out)
					@Override
					public void run() {
						mEngine.startNewGame();
						mEngine.printControllerName();
					}
				};
				timer = new Timer();
				timer.schedule(task, 600);
			} else if (mMenu.getOption().equals("Load Game")) {
				player.stop();
				Timer timer1;
				TimerTask task1 = new TimerTask() {
					// Delays the loading of a room (allowing the view to fade in and out)
					@Override
					public void run() {
						mEngine.loadGame();
					}
				};
				timer1 = new Timer();
				timer1.schedule(task1, 800);
				;
			} else if (mMenu.getOption().equals("Exit")) {
				Timer timer11;
				TimerTask task11 = new TimerTask() {
					// Delays the loading of a room (allowing the view to fade in and out)
					@Override
					public void run() {
						System.exit(0);
					}
				};
				timer11 = new Timer();
				timer11.schedule(task11, 800);
				System.out.println("Exiting Game");
			}
			mMenu.select();
			break;
		}
	}

	public void update() {
		// TODO Auto-generated method stub
	}
	
	public void p(String s){
		System.out.print("\t"+s+",");
	}
	
	public void pl(String s){
		System.out.println("");
	}

	public void playMusic(){
		if (!player.isPlaying()) {
			player = MediaPlayer.create(activity, R.raw.ff2prlde);
			player.start();
		}
	}

	@Override
	public View getView() {
		// TODO Auto-generated method stub
		return mTitleView;
	}

	@Override
	public void stopMusic() {
		// TODO Auto-generated method stub
		player.stop();
	}
}
